//
//  SelectMenuButton.m
//  Honda
//
//  Created by Wang.Xinlei on 4/24/13.
//  Copyright (c) 2013 HeninHoo. All rights reserved.
//

#import "ConfigCodeHelper.h"
#import "SelectMenuButton.h"

@implementation SelectMenuButton

- (void)dealloc {
    [self removeObserver:self forKeyPath:@"frame"];
    [self removeObserver:self forKeyPath:@"currentKey"];
    [self removeObserver:self forKeyPath:@"currentValue"];
    [self removeObserver:self forKeyPath:@"primaryKey"];

    SafeRelease(_currentKey);
    SafeRelease(_currentValue);
    
    SafeRelease(_primaryKey);
    SafeRelease(_contentArray);
    
    [super dealloc];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"frame"]) {
        [self setBackgroundImage:[[UIImage imageNamed:@"select_menu_button"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 30) resizingMode:UIImageResizingModeStretch]
                        forState:UIControlStateNormal];
    }
    
    if ([keyPath isEqualToString:@"currentKey"]) {
        NSString * value = nil;
        if ([NSString valid:self.currentKey]) {
            if ([NSString valid:self.primaryKey]) {
                value = [[ConfigCodeHelper shareInstance] codeAValueByPrimaryKey:self.primaryKey andSubKey:self.currentKey];
            }
            else {
                for (id object in self.contentArray) {
                    if ([NSString validString:object]) {
                        if ([object isEqualToString:self.currentKey]) {
                            value = object;
                            break;
                        }
                    }
                    else if ([NSDictionary validDictionary:object]) {
                        if ([[object objectForKey:@"key"] isEqualToString:self.currentKey]) {
                            value = [object objectForKey:@"value"];
                            break;
                        }
                    }
                }
            }
        }

        self.currentValue = value;
    }

    if ([keyPath isEqualToString:@"currentValue"]) {
        if ([NSString invalid:self.currentValue]) {
            [self setTitle:@"< 空 >" forState:UIControlStateNormal];
            [self setTitleColor:HexRGB(0x9f9f9f) forState:UIControlStateNormal];
        }
        else {
            [self setTitle:self.currentValue forState:UIControlStateNormal];
            [self setTitleColor:HexRGB(0x666666) forState:UIControlStateNormal];
        }
    }
    
    if ([keyPath isEqualToString:@"primaryKey"]) {
        self.contentArray = [[ConfigCodeHelper shareInstance] codeASystematicsByPrimaryKey:self.primaryKey];
        
        self.currentKey = self.currentKey;
    }
}

- (void)awakeFromNib {
    self.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 30);
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"currentKey" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"currentValue" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"primaryKey" options:NSKeyValueObservingOptionNew context:nil];
    
    [self addTarget:self action:@selector(onTapSelectMenuButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.frame = self.frame;
    self.primaryKey = self.primaryKey;
}

- (void)onTapSelectMenuButton:(UIButton *)button {
    if ([NSArray invalidArray:self.contentArray]) {
        return;
    }
    [SelectMenuViewController sharedController].from = button;
    [SelectMenuViewController sharedController].enableNilKey = self.enableNilKey;
    [[SelectMenuViewController sharedController] reloadSelectMenuWithContentArray:self.contentArray key:self.currentKey value:self.currentValue];
    [[UIApplication sharedApplication].keyWindow.rootViewController popViewController:[SelectMenuViewController sharedController] from:self completion:^{
        self.currentKey = [SelectMenuViewController sharedController].currentKey;
        self.currentValue = [SelectMenuViewController sharedController].currentValue;
    }];
}

@end
